﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace AzaEngine
{

    public struct RenameOptions
    {
        public bool ApplyInFolders;
        public bool ApplyInFiles;
        public bool ApplyInRootFolder;
    }

    public class RenameEngine
    {
        public RenameOptions Options;
        public Messages Messages = new Messages();

        public RenameEngine()
        {
            Options.ApplyInFolders = true;
            Options.ApplyInFiles = true;
            Options.ApplyInRootFolder = false;
        }

        /*private bool FitsMask(this string sFileName, string sFileMask)
        {
            Regex mask = new Regex(sFileMask.Replace(".", "[.]").Replace("*", ".*").Replace("?", "."));
            return mask.IsMatch(sFileName);
        }*/


        private void RunScripts(ref List<Folder> folders, List<Script> scripts)
        {
            ReplaceEngine rplEngine = new ReplaceEngine();
            string newFilename = string.Empty;

            for (int f = 0; f < folders.Count; f++)
            {
                // Folders
                if ((Options.ApplyInRootFolder && folders[f].Root) || (Options.ApplyInFolders && !folders[f].Root))
                {
                    newFilename = folders[f].FolderName;
                    if (rplEngine.RunScripts(ref newFilename, scripts) == ScriptReturn.Success)
                    {
                        folders[f].NewFolderName = newFilename;
                    }
                }

                // Files
                if (Options.ApplyInFiles)
                {
                    for (int file = 0; file < folders[f].Files.Count; file++)
                    {
                        newFilename = folders[f].Files[file].Filename;
                        if (rplEngine.RunScripts(ref newFilename, scripts) == ScriptReturn.Success)
                        {
                            folders[f].Files[file].NewFilename = newFilename;
                        }
                    }
                }
            }
        }

        public void Simulation(ref List<Folder> folders, List<Script> scripts)
        {
            RunScripts(ref folders, scripts);
        }

        // Efetiva o renomeio com com base no novo nome definido em Folder e File
        public void Rename(ref List<Folder> folders)
        {
            for (int f = 0; f < folders.Count; f++)
            {
                // Folders
                if ((Options.ApplyInRootFolder && folders[f].Root) || (Options.ApplyInFolders && !folders[f].Root))
                {
                    if (folders[f].ChangedName())
                    {
                        RenameFolder(folders[f]);
                        folders[f].ApplyRename();
                    }
                }

                // Files
                if (Options.ApplyInFiles)
                {
                    for (int file = 0; file < folders[f].Files.Count; file++)
                    {
                        if (folders[f].Files[file].ChangedName())
                        {
                            RenameFile(folders[f].Files[file]);
                            folders[f].Files[file].ApplyRename();
                        }
                    }
                }
            }
        }

        public void Rename(ref List<Folder> folders, List<Script> scripts)
        {
            RunScripts(ref folders, scripts);
            Rename(ref folders);
        }

        private void RenameFile(File file)
        {
            System.IO.File.Move(file.FilenameFull, file.NewFilenameFull);
        }


        private void RenameFolder(Folder folder)
        {
            Directory.Move(folder.FolderNameFull, folder.NewFolderNameFull);
        }
    }
}
